package com.yxh.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yxh.R;
import com.yxh.dto.LessonDto;
import com.yxh.service.Task;

public class ClassAddByInputActivity extends BaseActivity {

	private EditText et;
	private InputMethodManager inputMethodManager;
	@SuppressWarnings("unused")
	private int source; // 0 homeFragment右上角 1 homeFragment 菜单 2 二维码扫描

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_class_add_by_input);

		initView();

		initData();

		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	public void initView() {

		/*** top view ***/
		((TextView) findViewById(R.id.head_layout_tv)).setText("添加课程");

		et = (EditText) findViewById(R.id.et);
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				commitCode();

			}
		});

		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				inputMethodManager.toggleSoftInput(0,
						InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}, 150);

		overridePendingTransition(R.anim.enter_righttoleft, R.anim.noanim);
	}

	private void initData() {

	}

	private void commitCode() {
		String data = String.valueOf(et.getText());
		if (data.trim().equals("")) {
			showToast("请填写您的课程码");
			et.setFocusable(true);
			return;
		}

		if (data.trim().length() != 6) {
			showToast("请输入正确的6位课程码");
			et.setFocusable(true);
			return;
		}

		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("s", "lesson/get_lesson_packs");
		map.put("lesson_packs_id", data);
		getData(map, Task.GET_LESSON_PACKS, 1);

	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh(Object... param) {
		try {
			int operType = (Integer) param[0];
			Object obj = param[1];
			closeDialog();
			switch (operType) {
			case Task.GET_LESSON_PACKS:
				if (obj != null) {
					List<LessonDto> list = (ArrayList<LessonDto>) obj;
					if ("1".equals(list.get(0).result)) {
						Intent it = new Intent(mContext,
								ClassSelectActivity.class);
						it.putExtra("list", (Serializable) list);
						startActivity(it);
						finish();
					} else
						showToast(list.get(0).message);

				} else {
					showToast("网络连接异常,请检查网络");
				}
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}